/**
 * Copyright (c) 2016-2019 人人开源 All rights reserved.
 * <p>
 * https://www.answer.io
 * <p>
 * 版权所有，侵权必究！
 */

package com.ruoyi.answer.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.answer.common.annotation.Login;
import com.ruoyi.answer.entity.AdminUser;
import com.ruoyi.answer.entity.TestPaper;
import com.ruoyi.answer.entity.User;
import com.ruoyi.answer.form.RegisterForm;
import com.ruoyi.answer.service.AdminUserService;
import com.ruoyi.answer.service.TestPaperService;
import com.ruoyi.answer.service.UserService;
import com.ruoyi.answer.tool.R;
import com.ruoyi.answer.tool.Result;
import com.ruoyi.answer.tool.ValidatorUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import net.bytebuddy.implementation.bind.annotation.Default;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.boot.context.properties.bind.DefaultValue;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.Date;

/**
 * 注册接口
 *
 * @author Mark sunlightcs@gmail.com
 */
@RestController
@RequestMapping("/api")
@Api(tags = "注册接口")
@AllArgsConstructor
public class ApiUserController {

    public AdminUserService adminUserService;
    public UserService userService;

    @PostMapping("register")
    @ApiOperation("注册")
    @Login
    public R register(@Validated @RequestBody RegisterForm form, @RequestAttribute String account) {

        // ValidatorUtils.validateEntity(form);

        // AdminUser login = adminUserService.login(form.getAdminAccount(), form.getAdminPassword());
        // if (login == null) {
        //     return R.error("用户名或者密码错误");
        // }
        int number = form.getNumber();

        if (number < 1) {
            return R.ok("次数不可以小于1");
        }
        //if(number>4){
        //    number=4;
        //}
        boolean update1;
        if ("1".equals(form.getType())) {
            //只要30
            update1 = adminUserService.lambdaUpdate().setSql("balance = balance -" + (30)).eq(AdminUser::getAccount, account).ge(AdminUser::getBalance, (30)).update();

        } else if ("2".equals(form.getType())) {
            //一门三十
            update1 = adminUserService.lambdaUpdate().setSql("balance = balance -" + (30 * number)).eq(AdminUser::getAccount, account).ge(AdminUser::getBalance, (30 * number)).update();

        } else {
            return R.ok("类型不能为空");
        }

        if (!update1) {
            return R.ok("余额不足");
        }
        User user = userService.lambdaQuery().eq(User::getAccount, form.getAccount()).gt(User::getCreateTime, DateUtil.offsetDay(new Date(),-20))
                .one();
        if (null == user) {
            user = new User();
            user.setNumber(0);
        }
        int i = user.getNumber() + form.getNumber();
        BeanUtil.copyProperties(form, user);
        user.setNumber(i);
        user.setAdminAccount(account);
        user.setCreateTime(new Date());
        user.setStatus("1");

        boolean update = userService.lambdaUpdate().eq(User::getAccount, user.getAccount()).update(user);
        if (!update) {
            userService.save(user);
        }
        BigDecimal balance = adminUserService.lambdaQuery().eq(AdminUser::getAccount, account).one().getBalance();
        R r = new R();
        r.put("msg", "添加成功");
        r.put("balance", balance);

        return r;
    }

    @GetMapping("userList")
    @ApiOperation("列表")
    @Login
    public Result<IPage> list(@RequestParam(defaultValue = "1") Long current, @RequestAttribute String account) {

        // AdminUser login = adminUserService.login(adminAccount, adminPassword);
        // if (login == null) {
        //     adminAccount = "no";
        // }

        Page<User> page = new Page<User>();
        page.setSize(10);
        page.setCurrent(current);

        Page<User> page1 = userService.lambdaQuery()
                .ne(User::getStatus, "0")
                .eq(User::getAdminAccount, account)
                .gt(User::getCreateTime,DateUtil.offsetDay(new Date(),-7))
                .orderByDesc(User::getCreateTime).page(page);
        return new Result<IPage>(page1);
    }

    @GetMapping("user/{account}/{type}")
    @ApiOperation("查询")
    public Result<User> list(@PathVariable("account") String account, @PathVariable("type") String type) {
        if (type == null) {
            return new Result<>("500", "查询出错");
        }
        User one = userService.lambdaQuery().eq(User::getAccount, account)
                .eq(User::getType, type)
                .ne(User::getStatus, "0")
                .gt(User::getCreateTime, DateUtil.offsetDay(new Date(),-20))
                .last("limit 1").one();

        if (one != null) {
            return new Result<>(one);
        }
        return new Result<>("500", "查询出错");

    }
    /**
     * 充值余额
     */


}
